package com.cpp.task;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimerTask;

import weibo4j.model.Paging;
import weibo4j.model.Status;
import weibo4j.model.StatusWapper;
import weibo4j.model.WeiboException;

import com.cpp.bean.Tbl_Sina_Dzh;
import com.cpp.bean.Tbl_Sina_token;
import com.cpp.bean.Tbl_Sina_waccount;
import com.cpp.dao.Tbl_Sina_Dzh_DAO;
import com.cpp.dao.Tbl_Sina_waccount_DAO;
import com.cpp.util.SinaWeiboUtil;
import com.cpp.util.TokenUtil;

public class GetWeibo_TimerTask extends TimerTask {

	@Override
	public void run() {
		List<Tbl_Sina_waccount> tbl_sina_waccounts = new Tbl_Sina_waccount_DAO().query();
		Tbl_Sina_Dzh sina_dzh = null;
		for(Tbl_Sina_waccount waccount : tbl_sina_waccounts){
			Paging page = new Paging();
			page.setCount(10);//每次取10条
			page.setSinceId(Long.parseLong(waccount.getSinceid()));
			StatusWapper statusWapper = null;
			try {
				Tbl_Sina_token token = TokenUtil.getToken();
				statusWapper = new SinaWeiboUtil(token.getToken()).getUserTimelineByUid(waccount.getUid(), page);
			} catch (WeiboException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.println("token time out...................................");
				return;
			}
			if(statusWapper != null){
				List<Status> status = statusWapper.getStatuses();
				for(Status s : status){
					int level = 0;
					saveStatus(s,waccount,level);
					//sina_dzh.setType();
					
				}
			}
		}

	}
	
	private int saveStatus(Status s,Tbl_Sina_waccount waccount,int level){
		String uid = s.getUser().getId();
		String screenName = s.getUser().getScreenName();
		
		Date createdAt = s.getCreatedAt();
		String sid = s.getId();
		String mid = s.getMid();
		String text = s.getText();
		String sourceUrl = s.getSource().getUrl();
		String sourceName = s.getSource().getName();
		int favorited = 0;
		if(!s.isFavorited()){
			favorited = 1;
		}
		int truncated = 0;
		if(!s.isTruncated()){
			truncated = 1;
		}
		
		
		long inReplyToStatusId = s.getInReplyToStatusId();
		long inReplyToUserId = s.getInReplyToUserId();
		String inReplyToScreenName = s.getInReplyToScreenName();
		
		String thumbnailPic = s.getThumbnailPic();
		String bmiddlePic = s.getBmiddlePic();
		String originalPic = s.getOriginalPic();
		int retweetedStatus = 0;
		if(s.getRetweetedStatus() != null){			
			retweetedStatus = saveStatus(s.getRetweetedStatus(),waccount,level+1);			
		}
		
		
		String geo = s.getGeo();
		if(geo == null){
			geo = "";
		}
		String latitude = "";//s.getLatitude();
		String longitude = "";//s.getLongitude();
		int repostsCount = s.getRepostsCount();
		int commentsCount = s.getCommentsCount();
		String annotations = s.getAnnotations();
		Tbl_Sina_Dzh sina_dzh = new Tbl_Sina_Dzh(uid, screenName,createdAt, sid, mid,
				text, sourceUrl, sourceName,
				favorited, truncated, Long.toString(inReplyToStatusId), Long.toString(inReplyToUserId),inReplyToScreenName,
				thumbnailPic,bmiddlePic,originalPic,retweetedStatus,geo,latitude,longitude,repostsCount,commentsCount,annotations,waccount.getType(),level							
				);
		
		new Tbl_Sina_Dzh_DAO().saveOrUpdata(sina_dzh);
		long sinceid = Long.parseLong(sid);
		if(sinceid > Long.parseLong(waccount.getSinceid())){
			new Tbl_Sina_waccount_DAO().update(sinceid,waccount.getId());
		}		
		retweetedStatus = new Tbl_Sina_Dzh_DAO().queryId(sid); 
		return retweetedStatus;
	}

}
